<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addEBookTypeForm" ref="addEBookTypeRef" :rules="addEBookTyperules">
                        <el-form-item label="分类名称" :label-width="formLabelWidth"
                                      prop="name">
                            <el-input
                                    v-model="addEBookTypeForm.name"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写分类名称"
                            ></el-input>
                        </el-form-item>
                        <!-- <el-form-item label="级别名称" :label-width="formLabelWidth"
                                      prop="levelName">
                            <el-input
                                    v-model="addEBookTypeForm.levelName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写级别名称"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="级别" :label-width="formLabelWidth"
                                      prop="level" v-if="addEBookTypeForm.id">
                            <el-input
                                    v-model="addEBookTypeForm.level"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写级别"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="父id" :label-width="formLabelWidth"
                                      prop="parentId" v-if="addEBookTypeForm.id">
                            <el-input
                                    v-model="addEBookTypeForm.parentId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写父id"
                            ></el-input>
                        </el-form-item> -->
                        <el-form-item label="权重" :label-width="formLabelWidth"
                                      prop="sortNum">
                            <el-input
                                    v-model="addEBookTypeForm.sortNum"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写权重"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddEBookType",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addEBookTypeForm: {
                    name: "",
                    levelName: "",
                    level: "",
                    parentId: "",
                    sortNum: 1,
                },
                // 校验规则
                addEBookTyperules: {
                    name: vxRule(true, null, "blur"),
                    levelName: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getEBookTypeById(id) {
                httpService("/javaApi/eBookType/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addEBookTypeForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addEBookTypeForm.id = "";
                    this.addEBookTypeForm.name = "";
                    this.addEBookTypeForm.levelName = "";
                    this.addEBookTypeForm.level = "";
                    this.addEBookTypeForm.parentId = "";
                    this.addEBookTypeForm.sortNum = 1;
            },
            save() {
                this.$refs.addEBookTypeRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("name", this.addEBookTypeForm.name);
                        // params.append("levelName", this.addEBookTypeForm.levelName);
                        // params.append("level", this.addEBookTypeForm.level);
                        // params.append("parentId", this.addEBookTypeForm.parentId);
                        params.append("sortNum", this.addEBookTypeForm.sortNum);
                        if (this.addEBookTypeForm.id) {
                            params.append("id", this.addEBookTypeForm.id);
                            httpService("/javaApi/eBookType/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success(data.message);
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getEBookTypeList();
                                    } else {
                                        ElMessage.error(data.message);
                                    }
                                }
                            );
                        } else {
                            httpService("/javaApi/eBookType/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success(data.message);
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getEBookTypeList();
                                    } else {
                                        ElMessage.error(data.message);
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>